package com.qingxin.park.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.park.entity.Video;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;

@Service
public class VideoService extends BaseService<Video, String>{
	
	/**
	 * 视频列表数据
	 * @param pageParam
	 * @param search
	 * @param parkid
	 * @param request
	 * @return
	 */
	public Pager list(PageParam pageParam,String parkid, LoginUser loginUser, HttpServletRequest request){
		Querier querier = Querier.create("from Video");
		querier.append(" where parkid=:parkid ");
		querier.setParam("parkid", parkid);
		querier.append(" order by createTime desc");
		Pager  pager = pageByQuerier(querier, pageParam);
		return pager;
		
	}
	
	/**
	 * 添加
	 * @param wellProduct
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(Video video,LoginUser loginUser, HttpServletRequest request){
		Date now = new Date();
		video.setCreateTime(now);
		video.setCreateBy(loginUser.getLogin_name());
		if(StringUtils.isNotEmpty(video.getVideoLink())){
			UploadFile.moveTmpTo(video.getVideoLink(),UploadFile.DIR_PARK_VIDEO);
		}
		this.save(video);	
		return null;
	}
	
	/**
	 * 删除
	 * @param productid
	 * @param request
	 * @return
	 */
	public SysError delete(String[] ids, HttpServletRequest request){
		for(String id : ids){
			Video video = this.get(id);
			if(video == null){
				continue;
			}
			this.delete(video);
		}
		return null;
	}
	
	/**
	 * 修改
	 * @param wellProduct
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(Video video, LoginUser loginUser, HttpServletRequest request){
		Video old = getSafeEntity(video.getVideoid());
		if(StringUtils.isNotEmpty(video.getVideoLink()) && !video.getVideoLink().equals(old.getVideoLink())){
			UploadFile.moveTmpTo(video.getVideoLink(),UploadFile.DIR_PARK_VIDEO);
		}
		String fields = "videoLink,title,remark";
		BeanUtil.setTargetFields(old, video, fields, true);
		Updater<Video> updater = new Updater<>(old, fields);
		this.update(updater);
		return null;
	}
	
}
